<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_category extends CI_Model {

    var $cat_name;
    var $cat_admin;

    public function __construct() {
        parent::__construct();
        //$this->cat_admin = $_COOKIE['c_iduser'];
    }

    function set_cata_name($cat_name) {
        $this->cat_name = $cat_name;
    }

    function index() {
        
    }

    public function get_category_page($num, $offset){
        $sql = "SELECT * FROM category c "
                . "JOIN admin a ON c.id_admin = a.id_admin "
                . "ORDER BY id_category LIMIT $offset, $num";
        $data = $this->db->query($sql);
        $result = $data->result_array();

        return $result;
    }

    function get_all_category() {
        $sql = "SELECT * FROM category c
        JOIN admin a ON c.id_admin = a.id_admin
        ORDER BY category_name";
        $query = $this->db->query($sql);

        if ($query) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return false;
        }
    }

    public function insert_category($param){
        $id = get_cookie('c_id_adm');
        $sql = "INSERT INTO category (category_name, category_icon, category_color, id_admin, category_date) VALUES (?, ?, ?, $id, now())";
        $query = $this->db->query($sql, $param);

        if ($query) {
            return true;
        }else{
            return false;
        }
    }

    public function select_category_byID($param){
        $sql = "SELECT * FROM category c
        JOIN admin a ON c.id_admin = a.id_admin
        WHERE id_category = ?";
        $query = $this->db->query($sql, $param);

        if ($query) {
            $result = $query->row_array();
            return $result;
        } else {
            return false;
        }   
    }

    public function update_category($param){
        $sql = "UPDATE category SET category_name = ?, category_icon = ?, category_color = ? WHERE id_category = ?";
        $query = $this->db->query($sql, $param);

        if ($query) {
            return true;
        } else {
            return false;
        }   
    }

    public function delete_category($param){
        $sql = "DELETE FROM category WHERE id_category = ?";
        $query = $this->db->query($sql, $param);

        if ($query) {
            return true;
        } else {
            return false;
        }
    }
}
